package org.nowcoder.huawei.c100;

import java.util.HashMap;
import java.util.Map;

/**
 * 7:小朋友来自多少小区(计算班级里至少有几个小朋友)
 */

public class HC007 {
    public static void main(String[] args) {
        int[] garden = new int[]{2, 2, 3};
        System.out.println(solve(garden));

        int[] garden1 = new int[]{2, 2, 2, 3};
        System.out.println(solve(garden1));

        int[] garden2 = new int[]{2, 2, 2, 2, 3};
        System.out.println(solve(garden2));
    }

    public static int solve(int[] garden) {
        //边界条件
        if (garden.length == 1) {
            return garden[0];
        }
        Map<Integer, Integer> map = new HashMap<>(); //反馈相同的小朋友的数量
        for (int i = 0; i < garden.length; i++) {
            int n = map.getOrDefault(garden[i], 0);
            map.put(garden[i], n + 1);
        }

        //计算小朋友总数
        int result = 0;
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            int k = entry.getKey();
            int v = entry.getValue();
            result += (int) (Math.ceil((double) v / (k + 1)) * (k + 1));
        }
        return result;
    }
}
